package com.example.qmzy.ui.home;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

import com.example.qmzy.R;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;

public class HomeFragment extends Fragment {
    private FusedLocationProviderClient fusedLocationClient;
    private TextView weatherTextView;
    private TextView locationTextView;
    private TextView suggestionTextView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        
        weatherTextView = root.findViewById(R.id.weather_text);
        locationTextView = root.findViewById(R.id.location_text);
        suggestionTextView = root.findViewById(R.id.suggestion_text);
        
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(requireActivity());
        
        checkLocationPermission();
        
        return root;
    }

    private void checkLocationPermission() {
        if (ActivityCompat.checkSelfPermission(requireContext(), 
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        } else {
            getLocation();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 
                                         @NonNull int[] grantResults) {
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                getLocation();
            } else {
                Toast.makeText(requireContext(), "需要位置权限才能获取天气信息", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void getLocation() {
        if (ActivityCompat.checkSelfPermission(requireContext(), 
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            fusedLocationClient.getLastLocation()
                .addOnSuccessListener(requireActivity(), new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        if (location != null) {
                            updateLocationUI(location);
                            // TODO: 调用天气API获取天气信息
                            fetchWeatherData(location.getLatitude(), location.getLongitude());
                        }
                    }
                });
        }
    }

    private void updateLocationUI(Location location) {
        locationTextView.setText(String.format("当前位置：%.4f, %.4f", 
            location.getLatitude(), location.getLongitude()));
    }

    private void fetchWeatherData(double latitude, double longitude) {
        // TODO: 实现天气API调用
        // 这里需要实现具体的天气API调用逻辑
        weatherTextView.setText("正在获取天气信息...");
    }

    private void updateSuggestion(String weather, int aqi) {
        String suggestion;
        if (weather.contains("雨") || aqi > 100) {
            suggestion = "建议：今天天气不佳，建议室内活动";
        } else {
            suggestion = "建议：天气适宜，可以外出活动";
        }
        suggestionTextView.setText(suggestion);
    }
} 